package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     *
     * @param name
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<Clazz> listClazz(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        //构建分页对象
        Page<Clazz> p = new Page<>(page,pageSize);
        //根据修改时间进行倒序排序
        p.addOrder(OrderItem.desc("update_time"));

        //条件查询
        p = lambdaQuery().like(name != null && !name.isEmpty(), Clazz::getName, name)
                .between(begin != null && end != null, Clazz::getEndDate, begin, end)
                .page(p);
        //获取班级集合
        List<Clazz> clazzs = p.getRecords();
        //遍历班级 获取班主任id
        List<Long> empId = clazzs.stream().map(clazz -> clazz.getMasterId()).collect(Collectors.toList());
        if(!empId.isEmpty()){
            //调用empService方法，批量查询员工信息
            List<Emp> list = empService.listByIds(empId);
            //将班主任姓名封装到班级对象中
            clazzs.forEach(clazz -> {
                list.forEach(emp -> {
                    if(clazz.getMasterId() != null && clazz.getMasterId().equals(emp.getId())){
                        clazz.setMasterName(emp.getName());
                    }
                });
            });
        }
        //获取状态
            LocalDate now = LocalDate.now();
            clazzs.forEach(clazz -> {
                    if(now.isAfter(clazz.getBeginDate()) && now.isBefore(clazz.getEndDate())){
                        clazz.setStatus("在读");
                    }else if(now.isBefore(clazz.getEndDate())){
                            clazz.setStatus("未开课");
                        }else{
                            clazz.setStatus("已结课");
                    }
            });
        //返回一个PageResult
        return new PageResult<>(p.getTotal(),clazzs);
    }

    /**
     * 保存班级信息
     *
     * @param clazz
     */
    @Override
    public void saveClazz(Clazz clazz) {
        //补全基础代码
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);
    }

    /**
     * 根据id查询回显
     * @param id
     * @return
     */
    @Override
    public Clazz getByIdClazz(Integer id) {
        Clazz byId = getById(id);
        return byId;
    }

    /**
     * 修改班级信息
     * @param clazz
     */
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now()); //修改最后操作时间
        updateById(clazz);
    }

    /**
     * 删除班级信息
     *
     * @param id
     */
    @Override
    public void deteleClazz(Integer id) {
        remove(new LambdaQueryWrapper<Clazz>().in(Clazz::getId,id));
    }
}
